<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCardNo">
        <el-input v-model="queryParams.idCardNo" placeholder="请输入身份证号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-radio-group v-model="queryParams.auditStatus">
          <el-radio :label="null">全部</el-radio>
          <el-radio :label="0">未审核</el-radio>
          <el-radio :label="1">已审核</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="认证状态" prop="certificationStatus">
        <el-radio-group v-model="queryParams.certificationStatus">
          <el-radio :label="null">全部</el-radio>
          <el-radio :label="0">认证中</el-radio>
          <el-radio :label="1">认证成功</el-radio>
          <el-radio :label="2">认证失败</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="workercertificationauditList" @selection-change="handleSelectionChange">
      <el-table-column label="真实姓名" width="150" align="center" prop="name" />
      <el-table-column label="身份证号" width="200" align="center" prop="idCardNo" />
      <el-table-column label="身份证人像照片" width="180" align="center" prop="frontImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.frontImg" style="width: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="身份证国徽照片" width="180" align="center" prop="backImg">
        <template slot-scope="scope">
          <image-preview :src="scope.row.backImg" style="width: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="证明资料" align="center" prop="certificationMaterial" width="180"><template
          slot-scope="scope">
          <image-preview :src="scope.row.certificationMaterial" style="width: 50px" /> </template></el-table-column>
      <el-table-column label="审核状态" align="center" width="180" prop="auditStatus">
        <template slot-scope="scope">
          <el-tag :type="getAuditStatusText(scope.row.auditStatus) === '已审核'
            ? 'success'
            : 'danger'
            " plain>
            {{ getAuditStatusText(scope.row.auditStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核人" width="150" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.auditTime, "{y}-{m}-{d} {h}:{m}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="认证状态" align="center" prop="certificationStatus" width="180">
        <template slot-scope="scope">
          <el-tag :type="getCertificationStatusType(scope.row.certificationStatus)" plain>
            {{ getCertificationStatusText(scope.row.certificationStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" align="center" prop="rejectReason" width="150" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-check" @click="handleAccept(scope.row)"
            :disabled="scope.row.auditStatus === 1 && scope.row.certificationStatus === 1" style="width:64px"
            v-hasPermi="['workercertificationaudit:workercertificationaudit:edit']">通 过</el-button>
          <el-button size="mini" type="danger" icon="el-icon-close" @click="handleReject(scope.row)" style="width:64px"
            v-hasPermi="['workercertificationaudit:workercertificationaudit:remove']">驳 回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  listWorkercertificationaudit,
  updateWorkercertificationaudit,
} from "@/api/workercertificationaudit/workercertificationaudit";

export default {
  name: "Workercertificationaudit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务人员认证审核表格数据
      workercertificationauditList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        idCardNo: null,
        auditStatus: null,
        certificationStatus: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getAuditStatusText(status) {
      switch (status) {
        case 0:
          return "未审核";
        case 1:
          return "已审核";
        default:
          return "未知状态";
      }
    },

    getCertificationStatusText(status) {
      switch (status) {
        case 0:
          return "认证中";
        case 1:
          return "认证成功";
        case 2:
          return "认证失败";
        default:
          return "未知状态";
      }
    },

    getCertificationStatusType(status) {
      switch (status) {
        case 0:
          return "info"; // 灰色按钮
        case 1:
          return "success"; // 绿色按钮
        case 2:
          return "danger"; // 红色按钮
        default:
          return "info"; // 默认灰色按钮
      }
    },

    /** 查询服务人员认证审核列表 */
    getList() {
      this.loading = true;
      listWorkercertificationaudit(this.queryParams).then((response) => {
        this.workercertificationauditList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serveProviderId: null,
        name: null,
        idCardNo: null,
        frontImg: null,
        backImg: null,
        certificationMaterial: null,
        auditStatus: null,
        auditorId: null,
        auditorName: null,
        auditTime: null,
        certificationStatus: null,
        createTime: null,
        rejectReason: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      // 重置审核状态和认证状态为默认值
      this.queryParams.auditStatus = null;
      this.queryParams.certificationStatus = null;
      this.handleQuery();
    },

    handleAccept(row) {
      this.$confirm("是否通过该认证信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.updateStatus(row, {
            auditStatus: 1,
            certificationStatus: 1,
            rejectReason: "", // 清空驳回原因
          });
        })
        .catch(() => {
          // 取消操作
        });
    },

    handleReject(row) {
      this.$prompt("请输入驳回原因", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          this.updateStatus(row, {
            certificationStatus: 2,
            rejectReason: value,
          });
        })
        .catch(() => {
          // 取消操作
        });
    },

    updateStatus(row, data) {
      const updateData = {
        id: row.id,
        ...data,
      };
      updateWorkercertificationaudit(updateData).then((response) => {
        this.getList(); // 刷新列表
        this.$message({
          type: "success",
          message: "操作成功!",
        });
      });
    },
  },
};
</script>
